/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import utils.DbManager;
import utils.StoricoAsta;
import utils.UtenteIdProdUltimaOFF;
import utils.Utilizzatore;

/**
 *
 * @author Alice
 */
public class ControllaOfferte extends HttpServlet {
    private DbManager manager;
   
    @Override
public void init() throws ServletException {
// inizializza il DBManager dagli attributi di Application
this.manager = (DbManager)super.getServletContext().getAttribute("dbmanager");
}

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
          if(session.getAttribute("user")==null)
            response.sendRedirect(request.getContextPath() + "/error.jsp");
        List<StoricoAsta> sa=null;
        List<String> messaggi = new ArrayList<String>();
        System.out.println("SONO ENTRATO IN CONTROLLO OFFERTE");
          try{
         String username=(String)session.getAttribute("username");
         Utilizzatore user=(Utilizzatore)session.getAttribute("user");
         int utenteCorrente=user.getId_Utilizzatore();
         System.out.println("ok");
       
        /** List<UtenteIdProdUltimaOFF>listsa=(ArrayList<UtenteIdProdUltimaOFF>)session.getAttribute("ultimaOFF");
        if(listsa != null){
        System.out.println("NUOVA OFFERTA");
            for(int i=0;i<listsa.size();i++){
          int Utente=listsa.get(i).getIdUtente();
          int id_prod=listsa.get(i).getIdProdotto();
          float myOff=listsa.get(i).getUltimaOfferta();
          System.out.println("ok2,utente="+Utente+"idPro="+id_prod+",off="+myOff+",utenteCorr="+utenteCorrente);
          //if(Utente == utenteCorrente){
          System.out.println("ok3");
           */   //sa=manager.ControlloOfferte(id_prod, myOff,utenteCorrente);
              //RICEVO DATI DA ULTIMA OFFERTA
              //CONTROLLO CHE QST ASTA PARTECIPI O NO
              //L'UTENTE CORRENTE
              //SE PARTECIPA....ALLORA SCRIVO MESS...
              
         sa=manager.ControlloIdProd(utenteCorrente);
            if(sa!=null){
              
              for(int index=0;index<sa.size();index++){
               // if(id_prod == sa.get(index).getIdProdotto()){
                System.out.println("ok3;prodUGUALE");
                int idA=sa.get(index).getIdAsta();
                String nomeP=sa.get(index).getNomeProd();
                Timestamp time=sa.get(index).getDataOraOfferta();
                float off=sa.get(index).getOfferta();
                float ultimaOff=sa.get(index).getUltimaOff();
                float incr_min=sa.get(index).getIncrMin();
                System.out.println(idA+nomeP+time+off+incr_min);
                String mex="L'Asta n°"+idA+" del prodotto '"+nomeP+"' a cui Lei partecipa ha ricevuto una nuova Offerta di "+ultimaOff+" € . (Incremento minimo di: "+incr_min+" € )";
                messaggi.add(mex);
                System.out.println("ok4");
                } } 
                session.setAttribute("messaggiRialzo", messaggi);
        
          } catch (SQLException ex) {
            Logger.getLogger(ControllaOfferte.class.getName()).log(Level.SEVERE, null, ex);
            String message="Errore !";
            response.sendRedirect(request.getContextPath()+"/error.jsp?message"+URLEncoder.encode(message, "UTF-8"));
        }
        System.out.println("ok5");          
        response.sendRedirect(request.getContextPath()+"/Riepilogo");
         
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
